﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using Microsoft.VisualStudio.Modeling.Diagrams;
using System.IO;

namespace DSLFactory.VSCTDesigner
{
    class ImageTypeEditor : UITypeEditor
    {
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            NodeShape shape = context.Instance as NodeShape;
            Bitmap cmd = shape.ModelElement as Bitmap;

            string currentDirectory = Path.GetDirectoryName(ShellHelper.GetModelFileName(shape.Store));

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = currentDirectory;
            ofd.FileName = (string)value;

            if( ofd.ShowDialog() == DialogResult.OK)
            {
                string fileName = ofd.FileName;
                if( fileName.StartsWith(currentDirectory, StringComparison.CurrentCultureIgnoreCase) )
                    fileName = fileName.Substring(currentDirectory.Length + 1);
                value = fileName;
            }

            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}
